﻿using System;
using System.Collections.Generic;
using System.Data;
using System.Data.Common;
using System.Linq;
using System.Web;

using Microsoft.Practices.EnterpriseLibrary.Common;
using Microsoft.Practices.EnterpriseLibrary.Common.Configuration;
using Microsoft.Practices.EnterpriseLibrary.Data;
using Microsoft.Practices.EnterpriseLibrary.Logging;

namespace TractRecord
{
    /// <summary>
    /// Summary description for Download
    /// </summary>
    public class Download : IHttpHandler
    {

        public void ProcessRequest(HttpContext context)
        {
            GetFile(context.Request["id"], context);
        }

        public bool IsReusable
        {
            get
            {
                return false;
            }
        }


        public static void GetFile(string FileId, HttpContext context)
        {
            DbCommand cmd = Code.DataAccess.Data.GetStoredProcCommand("tr_ProjectFile_Get");
            cmd.Parameters.Add(new System.Data.SqlClient.SqlParameter("@FileId", FileId));

            //// Writes the BLOB to a file (*.bmp).
            //System.IO.MemoryStream stream;
            //// Streams the BLOB to the FileStream object.
            //System.IO.BinaryWriter writer;

            //// Size of the BLOB buffer.
            //int bufferSize = 100;
            //// The BLOB byte[] buffer to be filled by GetBytes.
            //byte[] outByte = new byte[bufferSize];
            //// The bytes returned from GetBytes.
            //long retval;
            //// The starting position in the BLOB output.
            //long startIndex = 0;

            IDataReader r = Code.DataAccess.Data.ExecuteReader(cmd);

            if (r.Read())
            {
                string contentType = r["MimeType"].ToString();
                string fileName = r["FileName"].ToString();

            //    // Create a file to hold the output.

            //    stream = new System.IO.MemoryStream();
            //    writer = new System.IO.BinaryWriter(stream);

            //    // Reset the starting byte for the new BLOB.
            //    startIndex = 0;

            //    // Read bytes into outByte[] and retain the number of bytes returned.
            //    retval = r.GetBytes(1, startIndex, outByte, 0, bufferSize);

            //    // Continue while there are bytes beyond the size of the buffer.
            //    while (retval == bufferSize)
            //    {
            //        writer.Write(outByte);
            //        writer.Flush();

            //        // Reposition start index to end of last buffer and fill buffer.
            //        startIndex += bufferSize;
            //        retval = r.GetBytes(1, startIndex, outByte, 0, bufferSize);
            //    }

            //    // Write the remaining buffer.
            //    writer.Write(outByte, 0, (int)retval - 1);
            //    writer.Flush();
                
            //    stream.Seek(0, System.IO.SeekOrigin.Begin);

                context.Response.ContentType = contentType;
                context.Response.AddHeader("Content-Disposition", "attachment; filename=" + fileName);
                context.Response.BinaryWrite((byte[])r["FileData"]);
            }
        }
    }
}